package com.tg.app;

import android.content.pm.ApplicationInfo;
import android.content.pm.PackageManager;
import android.util.Log;

import androidx.annotation.NonNull;

import com.appbase.custom.constant.CommonConstants;
import com.tange.base.toolkit.PreferenceUtil;
import com.tg.appcommon.android.TGApplicationBase;

import java.util.HashMap;
import java.util.Map;

public class CrashReportExtraMessageBuilder {

    private static final String TAG = "Crash-Report-Extra";

    public static Map<String, String> get() {
        Map<String, String>  map = new HashMap<>();
        try {

            Log.i(TAG, "get: ...");

            // 上报账号
            String account = PreferenceUtil.getString(TGApplicationBase.getApplicationContext(), CommonConstants.PRE_USER_NAME);
            Log.i(TAG, "get: account = " + account);
            map.put("user",account);
            String gitVersion = gitVersion();

            Log.i(TAG, "get: gitVersion = " + gitVersion);

            // 上报Git提交号
            map.put("gitVersion",gitVersion);
            Log.i(TAG, "get: extra = " + map.toString());

        } catch (Throwable ex) {
            Log.i(TAG, "get: error = " + ex);
        }
        return map;
    }

    @NonNull
    public static String gitVersion() {
        String gitVersion = "null";
        try {
            ApplicationInfo info = TGApplicationBase.getApplicationContext().getPackageManager().getApplicationInfo(
                    TGApplicationBase.getApplicationContext().getPackageName(), PackageManager.GET_META_DATA);
            gitVersion = info.metaData.get("com.tange.git.commit.version").toString();
        } catch (PackageManager.NameNotFoundException e) {
            e.printStackTrace();
            gitVersion = "null";
        }
        return gitVersion;
    }


}
